export default class PromiseAggregation {
  constructor() {
    this.promises = [];
  }

  append(key, promise) {
    this.promises.push([key, promise]);
    return this;
  }

  aggregate() {
    const arr = [];
    const keys = [];
    this.promises.forEach(([key, promise], index) => {
      arr[index] = promise;
      keys[index] = key;
    });

    return Promise.all(arr).then((list) => {
      const obj = {};
      list.forEach((res, index) => {
        const key = keys[index];
        obj[key] = res;
      });
      return obj;
    });
  }
}
